package com.zhsource.system.service.impl;

import cn.hutool.core.util.ObjUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhsource.system.domain.Employee;
import com.zhsource.system.domain.Logininfo;
import com.zhsource.system.mapper.EmployeeMapper;
import com.zhsource.system.mapper.LogininfoMapper;
import com.zhsource.system.query.EmployeeQuery;
import com.zhsource.system.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.LongFunction;
import java.util.stream.Collectors;

/**
 * <p>@ClassName：EmployeeServiceImpl
 * <p>@Date 2023/10/17  13:01
 *
 * @Author zzl
 **/
@Service
public class EmployeeServiceImpl implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public List<Employee> list() {
        return employeeMapper.list();
    }

    @Override
    public Employee selectById(Long id) {
        return employeeMapper.selectById(id);
    }

    //    @Override
//    public PageResult<List<Employee>> page(EmployeeQuery employeeQuery) {
////        // 1.查询总条数
//        Integer total = employeeMapper.total(employeeQuery);
//        // 2.查询当前页数据
//        List<Employee> page = employeeMapper.page(employeeQuery);
//
//        return  new PageResult<>(total,page);
//
//    }
    @Override
    public PageInfo<Employee> page(EmployeeQuery employeeQuery) {
        // 1.下面这句代码表示开启分页，会将紧挨着这句代码的下一次数据库执行进行分页，切记一定要放在要分页的SQL语句前面执行
        PageHelper.startPage(employeeQuery.getCurrentPage(), employeeQuery.getPageSize());

        // 2.执行SQL语句带条件查询，SQL语句不需要进行分页
        List<Employee> page = employeeMapper.page(employeeQuery);
        // 3.将查询结果转换为PageInfo对象，PageInfo对象中有前端分页所需要的各种参数
        PageInfo<Employee> pageInfo = new PageInfo<>(page);
        // 4.响应总条数和分页信息
        return pageInfo;

    }

    @Override
    public void update(Employee employee) {
        // 1. 修改用户信息
        employeeMapper.update(employee);
        Long id = employee.getId();
        // 2. 删除员工中间表信息
        employeeMapper.deleteEmployeeAndRoleByEmployeeId(id);
        // 3. 批量新增员工新增表信息
        List<Long> roleIds = employee.getRoleIds();
        if (roleIds != null && roleIds.size() > 0) {
            employeeMapper.batchinsertEmployeeAndRole(employee.getId(), employee.getRoleIds());
        }
        // 修改登录表信息
//
        // 从数据库中获取旧的员工信息
        Employee employee1 = employeeMapper.selectById(employee.getId());

        // 创建一个新的登录信息对象
        Logininfo logininfo = new Logininfo();
        logininfo.setId(employee1.getLogininfoId());
        logininfo.setUsername(employee.getUsername());
        logininfo.setPhone(employee.getPhone());
        logininfo.setEmail(employee.getEmail());
        // 根据员工状态设置登录信息的禁用状态
        if (employee.getState() == 1) {
            logininfo.setDisable(true);
        } else {
            logininfo.setDisable(false);
        }
        // 更新登录信息
        logininfoMapper.updateLoginInfo(logininfo);
        // 更新员工登录信息
        employeeMapper.updateLoginInfo(employee);
    }

    @Override
    public void insert(Employee employee) {

        // 1. 新增员工
        employeeMapper.insert(employee);

        List<Long> roleIds = employee.getRoleIds();
        if (roleIds != null && roleIds.size() > 0) {
            // 添加用户信息所对应的角色 ,如果角色集合为空则不添加
            employeeMapper.batchinsertEmployeeAndRole(employee.getId(), employee.getRoleIds());
        }

        // 同步维护logininfo表
        Logininfo logininfo = new Logininfo();
        logininfo.setUsername(employee.getUsername());
        logininfo.setPhone(employee.getPhone());
        logininfo.setEmail(employee.getEmail());
        logininfo.setSalt("RaVhRTiPfy2i1uZq7rMHEXsZYAWzu3VA");
        logininfo.setPassword("$2a$10$/7mXZXC/avBJ/ScPlGlXxe6bhX4b7XRB9.UQSk4kt6ePw.2YMIHoq");
        logininfo.setType(0);
        // 根据员工状态设置登录信息的禁用状态
        if (employee.getState() == 1) {
            logininfo.setDisable(true);
        } else {
            logininfo.setDisable(false);
        }
        // 插入或更新登录信息
        logininfoMapper.insertOrUpdateLoginInfo(logininfo);
        // 设置员工的登录信息ID
        employee.setLogininfoId(logininfo.getId());
        // 更新员工信息
        employeeMapper.update(employee);

    }
    @Override
    public void delete(Long id) {
        Employee employee = employeeMapper.selectById(id);
        Long logininfoId = employee.getLogininfoId();
        employeeMapper.delete(id);
        employeeMapper.deleteByEmployeeId(id);
        logininfoMapper.deleteLoginInfo(logininfoId);


    }

    @Override
    public void batchDel(List<Long> ids) {
//        List<Employee> list = employeeMapper.list();
//        Map<Long, Employee> collect = list.stream().collect(Collectors.toMap(Employee::getLogininfoId, e -> e));
//        List<Long> logininfoIds = new ArrayList<>();
//        for (Long id : ids) {
//            Employee employee = employeeMapper.selectById(id);
//            if (!ObjUtil.isEmpty(employee)){
//                logininfoIds.add(employee.getLogininfoId());
//            }
//        }
//        employeeMapper.batchDelLogininfo(logininfoIds);
//        employeeMapper.batchDel(ids);
        employeeMapper.batchDelLogininfo(ids);
        employeeMapper.batchDeletByEmployeeIds(ids);

    }
}
